<?php
//putenv("DISPLAY=:1");
//putenv("XAUTHORITY=/var/www/.Xauthority");

ini_set('display_errors', 1);
error_reporting(E_ALL);

//include 'Bot.php';
include 'RedmineApi.php';

$api = new RedmineApi();
$name = 'dsfasdfadfgsdfgsdfsdf';
$idProject = 21;

$data = array(
    'issue' => array(
        'project_id'  => intval($idProject),
        'subject'     => $name,
    )
);
//$data = file_get_contents('1.xml');
//var_dump($data);
//$xml = arrayToXml($data);
//var_dump($xml);

$url = 'http://pm.vs58.net/issues.xml';
$method = 'POST';

$authorization = base64_encode('stats' . ':' . 'Pass@word1');


var_dump($authorization);
$options = array(
    CURLOPT_URL            => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => array(
        'Content-Type: application/xml',
        'Authorization: Basic ' . $authorization
    ),
);

$options[CURLOPT_POST]       = true;
$options[CURLOPT_POSTFIELDS] = $data;

$curl = curl_init();
curl_setopt_array($curl, $options);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
var_dump($options);
$end = microtime(true);

function arrayToXml($array, $rootElement = null, $xml = null) {
    $_xml = $xml;

    if ($_xml === null) {
        $_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '</issue>');
    }

    foreach ($array as $k => $v) {
        if (is_array($v)) { //nested array
            arrayToXml($v, $k, $_xml->addChild($k));
        } else {
            $_xml->addChild($k, $v);
        }
    }

    return $_xml->asXML();
}


/*
 *
 *

$params = '{"issue":{"project_id":"2","subject":"Testing error"}}';
                $ch = curl_init();
                curl_setopt ($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS,  $params);
                curl_setopt ($ch, CURLOPT_HTTPHEADER, array ("Content-Type: application/json", "Length: " . strlen ($params)));
                curl_setopt ($ch, CURLOPT_USERPWD, "admin:password");

                curl_setopt ($ch, CURLOPT_URL, "http://your.redmine.com:3000/issues.json");
                curl_setopt ($ch, CURLOPT_MAXREDIRS, 3);
                curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($ch, CURLOPT_VERBOSE, 0);
                curl_setopt ($ch, CURLOPT_HEADER, 1);
                curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);

                $a = curl_exec($ch);
                curl_close($ch);
 */